package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.icss.beans.HotNewsVO;
import com.icss.beans.NewsTypeVO;
import com.icss.daos.HotNewsDao;

public class HotNewsServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public HotNewsServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String action = request.getParameter("action");
		HotNewsDao hndao = new HotNewsDao();
		if("show".equals(action)){
			request.getRequestDispatcher("/page/index/hotNews/showNews.jsp").forward(request, response);
		}else if("showNews".equals(action)){
			List<HotNewsVO> list = hndao.getAllNews();
			request.setAttribute("newsList", list);
			request.getRequestDispatcher("/page/index/hotNews/manageNews.jsp").forward(request, response);
		}else if("addNews".equals(action)){
			List<NewsTypeVO> list = hndao.getType();
			request.setAttribute("typeList", list);
			
			request.getRequestDispatcher("/page/index/hotNews/addNews.jsp").forward(request, response);
		}else if("add".equals(action)){
			String title=request.getParameter("newsTitle");
			int typeID = Integer.parseInt(request.getParameter("typeID"));
			String context = request.getParameter("newsContext");
			
			HotNewsVO vo = new HotNewsVO();
			vo.setNewsContext(context);
			vo.setNewsTitle(title);
			vo.setTypeID(typeID);
			hndao.addNews(vo);
			request.getRequestDispatcher("HotNewsServlet?action=showNews").forward(request, response);			
			
		}else if("edit".equals(action)){
			int newsID = Integer.parseInt(request.getParameter("newsID"));
			HotNewsVO vo = hndao.GetOneNews(newsID);
			request.setAttribute("newsvo", vo);
			List<NewsTypeVO> list = hndao.getType();
			request.setAttribute("typeList", list);
			request.getRequestDispatcher("/page/index/hotNews/editNews.jsp").forward(request, response);
		}else if("update".equals(action)){
			int newsID = Integer.parseInt(request.getParameter("newsID"));
			String title=request.getParameter("newsTitle");
			int typeID = Integer.parseInt(request.getParameter("typeID"));
			String context = request.getParameter("newsContext");
			
			HotNewsVO vo = new HotNewsVO();
			vo.setNewsContext(context);
			vo.setNewsTitle(title);
			vo.setTypeID(typeID);
			vo.setNewsID(newsID);
			hndao.updateNews(vo);
			request.getRequestDispatcher("HotNewsServlet?action=showNews").forward(request, response);
			
		}else if("del".equals(action)){
			int newsID = Integer.parseInt(request.getParameter("newsID"));
			hndao.delNews(newsID);
			request.getRequestDispatcher("HotNewsServlet?action=showNews").forward(request, response);
		}else if("delAllSelect".equals(action)){
			String str = request.getParameter("uidList");
			hndao.delAllSelect(str);
			request.getRequestDispatcher("HotNewsServlet?action=showNews").forward(request, response);
		}else if("display".equals(action)){
			int newsID = Integer.parseInt(request.getParameter("newsID"));
			HotNewsVO vo =hndao.GetOneNews(newsID);
			request.setAttribute("newsvo", vo);
			request.getRequestDispatcher("/page/index/hotNews/displayNews.jsp").forward(request, response);
		}else if("displayNews".equals(action)){
			int newsID = Integer.parseInt(request.getParameter("newsID"));
			HotNewsVO vo =hndao.GetOneNews(newsID);
			List<HotNewsVO> otherNews = hndao.getotherNews(vo);
			if(otherNews.size()>0){
				request.setAttribute("isHave", "y");
				request.setAttribute("otherNews", otherNews);
			}else{
				request.setAttribute("isHave", "n");
			}
			
			
			request.setAttribute("newsvo", vo);
			request.getRequestDispatcher("/page/index/hotNews/indexDisplay.jsp").forward(request, response);
		}else if("showType".equals(action)){
			List<NewsTypeVO> list = hndao.getType();
			request.setAttribute("typeList", list);
			request.getRequestDispatcher("/page/index/hotNews/showType.jsp").forward(request, response);
		}else if("updateType".equals(action)){
			String[] typeNames = request.getParameterValues("typeName");
//			String typeName=request.getParameter("typeName");
			String[] typeIDs = request.getParameterValues("typeID");
			String[] remarks = request.getParameterValues("remark");
//			System.out.println(typeName);
//			System.out.println(typeID);
//			System.out.println(remark);
			hndao.updateType(typeNames,typeIDs,remarks);
			request.getRequestDispatcher("HotNewsServlet?action=showType").forward(request, response);
		}else if("second".equals(action)){
			int typeID = Integer.parseInt(request.getParameter("typeID"));
			if(typeID==0){
				List<HotNewsVO> list = hndao.getAllNews();
				NewsTypeVO ntvo = hndao.getOneNewsType(typeID);
				ntvo.setTypeName("学子园地");
				ntvo.setRemark("中软卓越具备完善强大的就业体系，北京就业合作企业：来自中软集团和60余家知名软件企业的真实就业机会;中软卓越具备完善强大的就业体系，北京就业合作企业：来自中软集团和60余家知名软件企业的真实就业机会;中软卓越具备完善强大的就业体系，北京就业合作企业：来自中软集团和60余家知名软件企业的真实就业机会;中软国际卓越培训中心拥有一支技术高超、经验丰富的IT培训师资队伍，在业界享有盛誉……　");
				request.setAttribute("newsList", list);
				request.setAttribute("newsType", ntvo);
			}else{
				List<HotNewsVO> list = hndao.getTypeNews(typeID);
				NewsTypeVO ntvo = hndao.getOneNewsType(typeID);
				request.setAttribute("newsList", list);
				request.setAttribute("newsType", ntvo);
			}
			
			request.getRequestDispatcher("/page/index/hotNews/stuLift.jsp").forward(request, response);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
